package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.common.globalException.LeaseException;
import com.atguigu.lease.common.result.ResultCodeEnum;
import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.model.enums.ReleaseStatus;
import com.atguigu.lease.web.admin.mapper.*;
import com.atguigu.lease.web.admin.service.*;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentDetailVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentItemVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentQueryVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentSubmitVo;
import com.atguigu.lease.web.admin.vo.fee.FeeValueVo;
import com.atguigu.lease.web.admin.vo.graph.GraphVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【apartment_info(公寓信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class ApartmentInfoServiceImpl extends ServiceImpl<ApartmentInfoMapper, ApartmentInfo>
        implements ApartmentInfoService {

    @Autowired
    private ApartmentInfoMapper apartmentInfoMapper;
    @Autowired
    private GraphInfoService graphInfoService;//图片
    @Autowired
    private GraphInfoMapper graphInfoMapper;

    @Autowired
    private ApartmentFacilityService apartmentFacilityService;//配套
    @Autowired
    private ApartmentFacilityMapper apartmentFacilityMapper;

    @Autowired
    private ApartmentFeeValueService apartmentFeeValueService;//杂费
    @Autowired
    private ApartmentFeeValueMapper apartmentFeeValueMapper;

    @Autowired
    private ApartmentLabelService apartmentLabelService;//标签
    @Autowired
    private ApartmentLabelMapper apartmentLabelMapper;

    @Autowired
    private RoomInfoService roomInfoService;

    // 分页查询公寓列表
    public IPage<ApartmentItemVo> pageItem(Page<ApartmentItemVo> itemVoPage, ApartmentQueryVo queryVo) {
        return apartmentInfoMapper.pageItem(itemVoPage, queryVo);
    }

    // 保存或更新公寓信息
    public void saveOrUpdateApartment(ApartmentSubmitVo apartmentSubmitVo) {
        //判断是保存还是更新
        if (apartmentSubmitVo.getId() != null) {
            //更新

            //1.根据公寓id删除对应图片
            LambdaQueryWrapper<GraphInfo> graphWapper = new LambdaQueryWrapper<>();
            graphWapper.eq(GraphInfo::getItemId, apartmentSubmitVo.getId());
            graphWapper.eq(GraphInfo::getItemType, ItemType.APARTMENT);
            graphInfoService.remove(graphWapper);

            //2.根据公寓id删除与配套关联表的数据
            LambdaQueryWrapper<ApartmentFacility> facilityWapper = new LambdaQueryWrapper<>();
            facilityWapper.eq(ApartmentFacility::getApartmentId, apartmentSubmitVo.getId());
            apartmentFacilityService.remove(facilityWapper);

            //3.根据公寓id删除与杂费关联表的数据
            LambdaQueryWrapper<ApartmentFeeValue> feeValueWapper = new LambdaQueryWrapper<>();
            feeValueWapper.eq(ApartmentFeeValue::getApartmentId, apartmentSubmitVo.getId());
            apartmentFeeValueService.remove(feeValueWapper);

            //4.根据公寓id删除与标签关联表的数据
            LambdaQueryWrapper<ApartmentLabel> labelWapper = new LambdaQueryWrapper<>();
            labelWapper.eq(ApartmentLabel::getApartmentId, apartmentSubmitVo.getId());
            apartmentLabelService.remove(labelWapper);
        }
        //先更新公寓基本
        super.saveOrUpdate(apartmentSubmitVo);

        //1.插入图片列表
        List<GraphVo> graphVoList = apartmentSubmitVo.getGraphVoList();
        List<GraphInfo> graphInfoList = new ArrayList<>();
        for (GraphVo graphVo : graphVoList) {
            GraphInfo graphInfo = new GraphInfo();
            graphInfo.setName(graphVo.getName());
            graphInfo.setUrl(graphVo.getUrl());
            graphInfo.setItemId(apartmentSubmitVo.getId());
            graphInfo.setItemType(ItemType.APARTMENT);
            graphInfoList.add(graphInfo);
        }
        graphInfoService.saveBatch(graphInfoList);

        //2.插入配套列表
        List<Long> facilityInfoIds = apartmentSubmitVo.getFacilityInfoIds();
        List<ApartmentFacility> apartmentFacilityList = new ArrayList<>();
        for (Long facilityInfoId : facilityInfoIds) {
            ApartmentFacility apartmentFacility = new ApartmentFacility();
            apartmentFacility.setApartmentId(apartmentSubmitVo.getId());
            apartmentFacility.setFacilityId(facilityInfoId);
            apartmentFacilityList.add(apartmentFacility);
        }
        apartmentFacilityService.saveBatch(apartmentFacilityList);

        //3.插入标签列表
        List<Long> labelIds = apartmentSubmitVo.getLabelIds();
        List<ApartmentLabel> apartmentLabelList = new ArrayList<>();
        for (Long labelId : labelIds) {
            ApartmentLabel apartmentLabel = new ApartmentLabel();
            apartmentLabel.setApartmentId(apartmentSubmitVo.getId());
            apartmentLabel.setLabelId(labelId);
            apartmentLabelList.add(apartmentLabel);
        }
        apartmentLabelService.saveBatch(apartmentLabelList);

        //4.插入杂费列表
        List<Long> feeValueIds = apartmentSubmitVo.getFeeValueIds();
        List<ApartmentFeeValue> apartmentFeeValueList = new ArrayList<>();
        for (Long feeValueId : feeValueIds) {
            ApartmentFeeValue apartmentFeeValue = new ApartmentFeeValue();
            apartmentFeeValue.setApartmentId(apartmentSubmitVo.getId());
            apartmentFeeValue.setFeeValueId(feeValueId);
            apartmentFeeValueList.add(apartmentFeeValue);
        }
        apartmentFeeValueService.saveBatch(apartmentFeeValueList);
    }

    //根据id获取公寓详细信息
    public ApartmentDetailVo getApartmentDetail(Long id) {
        //1.根据id查询公寓基本信息
        ApartmentInfo apartmentInfo = super.getById(id);

        //2.根据公寓id查询图片列表
        List<GraphVo> graphVoList =graphInfoMapper.getByItemIdAndItemType(id, ItemType.APARTMENT);

        //3.根据公寓id查询配套列表
        List<FacilityInfo> facilityInfoList = apartmentFacilityMapper.getByApartmentId(id);

        //4.根据公寓id查询标签列表
        List<LabelInfo> labelInfoList = apartmentLabelMapper.getByApartmentId(id);

        //5.根据公寓id查询杂费列表
        List<FeeValueVo> feeValueVoList = apartmentFeeValueMapper.getByApartmentId(id);

        //封装数据
        ApartmentDetailVo apartmentDetailVo = new ApartmentDetailVo();
        BeanUtils.copyProperties(apartmentInfo, apartmentDetailVo);
        apartmentDetailVo.setGraphVoList(graphVoList);
        apartmentDetailVo.setFacilityInfoList(facilityInfoList);
        apartmentDetailVo.setLabelInfoList(labelInfoList);
        apartmentDetailVo.setFeeValueVoList(feeValueVoList);

        return apartmentDetailVo;
    }

    //根据id删除公寓信息
    public void delete(Long id) {
        //1.根据id删除公寓基本信息
        //先判断是否有房间存在，如果有就报错：请先删除该公寓下的所有房间
        LambdaQueryWrapper<RoomInfo> roomInfoWapper = new LambdaQueryWrapper<>();
        roomInfoWapper.eq(RoomInfo::getApartmentId, id);
        long count = roomInfoService.count(roomInfoWapper);
        if (count > 0) {
            throw new LeaseException(ResultCodeEnum.ADMIN_APARTMENT_DELETE_ERROR);
        }
        //没有则根据id删除公寓信息
        super.removeById(id);

        //2.根据公寓id删除图片列表
        LambdaQueryWrapper<GraphInfo> graphWapper = new LambdaQueryWrapper<>();
        graphWapper.eq(GraphInfo::getItemId, id);
        graphWapper.eq(GraphInfo::getItemType, ItemType.APARTMENT);
        graphInfoService.remove(graphWapper);

        //3.根据公寓id删除配套列表
        LambdaQueryWrapper<ApartmentFacility> facilityWapper = new LambdaQueryWrapper<>();
        facilityWapper.eq(ApartmentFacility::getApartmentId, id);
        apartmentFacilityService.remove(facilityWapper);

        //4.根据公寓id删除标签列表
        LambdaQueryWrapper<ApartmentLabel> labelWapper = new LambdaQueryWrapper<>();
        labelWapper.eq(ApartmentLabel::getApartmentId, id);
        apartmentLabelService.remove(labelWapper);

        //5.根据公寓id删除杂费列表
        LambdaQueryWrapper<ApartmentFeeValue> feeValueWapper = new LambdaQueryWrapper<>();
        feeValueWapper.eq(ApartmentFeeValue::getApartmentId, id);
        apartmentFeeValueService.remove(feeValueWapper);
    }

    //根据id修改公寓发布状态
    public void updateReleaseStatusById(Long id, ReleaseStatus status) {
        apartmentInfoMapper.updateReleaseStatusById(id, status);
    }

    //根据区域id查询公寓信息列表
    public List<ApartmentInfo> listInfoByDistrictId(Long id) {
        List<ApartmentInfo> list = apartmentInfoMapper.listInfoByDistrictId(id);
        return list;
    }
}




